﻿namespace Cassini
{
    using Cassini.Properties;
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;
    using AxPHONIC_USBLib;
    using MRecorder;
    using System.Collections;
    using Ant.Component;
    using System.Net;
    using System.Text;
    using System.Runtime.InteropServices;

    internal class MainForm : Form
    {
        private static string _appPath = string.Empty;
        private static int _portString = 8102;
        private Server _server;
        private static string _virtRoot = "/";
        private ArrayList ChannelArray;
        private AxPHONIC_USBLib.AxPhonic_usb Phonic_usb;
        private ListView Linelist;
        private NotifyIcon notifyIcon1;
        private System.ComponentModel.IContainer components;
        private Label label1;
        private LinkLabel linkLabel1;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem 显示ToolStripMenuItem;
        private ToolStripMenuItem 隐藏ToolStripMenuItem;
        private ToolStripMenuItem 退出ToolStripMenuItem;
        private Label label2;



        private string TanChuString = string.Empty;//弹出窗口显示的号码，用来做对比，跟获取的phone不一致就弹出
        public MainForm(string[] args)
        {


            this.InitializeComponent();

        }

        private string GetLinkText()
        {
            string str = "http://localhost";
            if (_portString != 80)
            {
                str = str + ":" + _portString;
            }
            str = str + _virtRoot;
            if (!str.EndsWith("/"))
            {
                str = str + "/";
            }
            return str;
        }


        [STAThread]
        private static void Main(string[] args)
        {
            //只允许一个示例运行
            bool createNew;
            using (System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out createNew))
            {
                if (createNew)
                {
                    Application.Run(new MainForm(args));
                }
                else
                {
                    //MessageBox.Show("应用程序已经在运行中...");
                    // System.Threading.Thread.Sleep(1000);
                    System.Environment.Exit(1);
                }
            }

        }

        private void ShowError(string err)
        {
            MessageBox.Show(err, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void ShowBalloonTip(string text)
        {
            this.notifyIcon1.ShowBalloonTip(1, this.Text, text, ToolTipIcon.Info);
            // base.Click += new EventHandler(this.notifyIcon1_Click);
        }




        void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {

                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
                this.TopMost = true;
            }


        }
        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            if (base.WindowState == FormWindowState.Minimized)
            {
                base.Hide();
                this.notifyIcon1.Visible = true;
            }
            if (base.WindowState == FormWindowState.Maximized)
            {
            }
        }


        private void MainForm_Closing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("程序正在使用中,确认退出?", "确认退出", MessageBoxButtons.YesNo))
            {
                this.Phonic_usb.CloseDevice();

                Process[] allProcess = Process.GetProcessesByName("IEXPLORE");
                foreach (Process pc in allProcess)
                {

                    if (pc.MainWindowTitle.Contains("酒店好帮手") || pc.MainWindowTitle.Contains("来话宝"))
                    {
                        pc.Kill();
                    }
                }
            }
            else
            {
                e.Cancel = true;
            }
        }
        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.Activate();
            this.TopMost = true;
        }

        private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.notifyIcon1.Visible = false;
            this.Close();
            this.Dispose();
            Application.Exit();
        }



        private void Start()
        {
            _appPath = Application.StartupPath.ToLower();

            if ((_appPath.Length == 0) || !Directory.Exists(_appPath))
            {
                this.ShowError("路径有错误");

            }
            else
            {
                _portString = 8102;
                _virtRoot = "/";
                try
                {
                    this._server = new Server(_portString, _virtRoot, _appPath);
                    this._server.Start();
                    System.Threading.Thread.Sleep(3000);
                    Process.Start("IEXPLORE.EXE", this.GetLinkText() + this.GetSSOLogin());//自动打开IE
                }
                catch
                {
                    this.ShowError("端口被占用 " + _portString + ".\r\n请先停止占用本端口的程序");

                    return;
                }



            }



        }

        //private void Stop()
        //{
        //    try
        //    {
        //        if (this._server != null)
        //        {
        //            this._server.Stop();
        //        }
        //    }
        //    catch
        //    {
        //    }
        //    finally
        //    {
        //        this._server = null;
        //    }
        //    base.Close();
        //}
        private string GetSSOLogin()
        {
            return "ui/sso.aspx";
        }
        private string FirstPages()
        {
            string[] strArray = new string[] { "install.aspx", "main.aspx", "default.aspx", "index.aspx", "default.htm", "default.html", "index.htm", "index.html" };
            for (int i = 0; i < strArray.Length; i++)
            {
                if (File.Exists(_appPath + strArray[i]))
                {
                    return strArray[i];
                }
            }
            return "";
        }

        #region 强制结束进程
        [DllImport("kernel32.dll")]
        private static extern void ExitProcess(uint uExitCode);
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);
        #endregion

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
            this.Phonic_usb = new AxPHONIC_USBLib.AxPhonic_usb();
            this.Linelist = new System.Windows.Forms.ListView();
            this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.显示ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.隐藏ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.label1 = new System.Windows.Forms.Label();
            this.linkLabel1 = new System.Windows.Forms.LinkLabel();
            this.label2 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.Phonic_usb)).BeginInit();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // Phonic_usb
            // 
            this.Phonic_usb.Enabled = true;
            this.Phonic_usb.Location = new System.Drawing.Point(429, 12);
            this.Phonic_usb.Name = "Phonic_usb";
            this.Phonic_usb.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("Phonic_usb.OcxState")));
            this.Phonic_usb.Size = new System.Drawing.Size(11, 16);
            this.Phonic_usb.TabIndex = 0;
            this.Phonic_usb.Visible = false;
            this.Phonic_usb.PlugOut += new AxPHONIC_USBLib._DPhonic_usbEvents_PlugOutEventHandler(this.OnPlugOut);
            this.Phonic_usb.PLugIn += new AxPHONIC_USBLib._DPhonic_usbEvents_PLugInEventHandler(this.OnPlugIn);
            this.Phonic_usb.HookOff += new AxPHONIC_USBLib._DPhonic_usbEvents_HookOffEventHandler(this.OnHookOff);
            this.Phonic_usb.HangUp += new AxPHONIC_USBLib._DPhonic_usbEvents_HangUpEventHandler(this.OnHangup);
            this.Phonic_usb.Ring += new AxPHONIC_USBLib._DPhonic_usbEvents_RingEventHandler(this.OnRing);
            this.Phonic_usb.CallId += new AxPHONIC_USBLib._DPhonic_usbEvents_CallIdEventHandler(this.OnCallId);
            this.Phonic_usb.DtmfKey += new AxPHONIC_USBLib._DPhonic_usbEvents_DtmfKeyEventHandler(this.OnDtmfKey);
            this.Phonic_usb.RingCancel += new AxPHONIC_USBLib._DPhonic_usbEvents_RingCancelEventHandler(this.OnRingCancel);
            this.Phonic_usb.PlayEnd += new AxPHONIC_USBLib._DPhonic_usbEvents_PlayEndEventHandler(this.OnPlayEnd);
            this.Phonic_usb.DeviceError += new AxPHONIC_USBLib._DPhonic_usbEvents_DeviceErrorEventHandler(this.OnDeviceError);
            this.Phonic_usb.DeviceAlarm += new AxPHONIC_USBLib._DPhonic_usbEvents_DeviceAlarmEventHandler(this.OnDeviceAlarm);
            this.Phonic_usb.CallOutFinish += new AxPHONIC_USBLib._DPhonic_usbEvents_CallOutFinishEventHandler(this.OnCallOutFinish);
            // 
            // Linelist
            // 
            this.Linelist.Location = new System.Drawing.Point(12, 12);
            this.Linelist.Name = "Linelist";
            this.Linelist.Size = new System.Drawing.Size(411, 124);
            this.Linelist.TabIndex = 1;
            this.Linelist.UseCompatibleStateImageBehavior = false;
            // 
            // notifyIcon1
            // 
            this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
            this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
            this.notifyIcon1.Text = "酒店好帮手";
            this.notifyIcon1.Visible = true;
            this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_DoubleClick);
            this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.显示ToolStripMenuItem,
            this.隐藏ToolStripMenuItem,
            this.退出ToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(95, 70);
            // 
            // 显示ToolStripMenuItem
            // 
            this.显示ToolStripMenuItem.Name = "显示ToolStripMenuItem";
            this.显示ToolStripMenuItem.Size = new System.Drawing.Size(94, 22);
            this.显示ToolStripMenuItem.Text = "显示";
            this.显示ToolStripMenuItem.Click += new System.EventHandler(this.显示ToolStripMenuItem_Click);
            // 
            // 隐藏ToolStripMenuItem
            // 
            this.隐藏ToolStripMenuItem.Name = "隐藏ToolStripMenuItem";
            this.隐藏ToolStripMenuItem.Size = new System.Drawing.Size(94, 22);
            this.隐藏ToolStripMenuItem.Text = "隐藏";
            this.隐藏ToolStripMenuItem.Click += new System.EventHandler(this.隐藏ToolStripMenuItem_Click);
            // 
            // 退出ToolStripMenuItem
            // 
            this.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem";
            this.退出ToolStripMenuItem.Size = new System.Drawing.Size(94, 22);
            this.退出ToolStripMenuItem.Text = "退出";
            this.退出ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 143);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(161, 12);
            this.label1.TabIndex = 2;
            this.label1.Text = "运行过程中请不要关闭本程序";
            // 
            // linkLabel1
            // 
            this.linkLabel1.AutoSize = true;
            this.linkLabel1.Location = new System.Drawing.Point(10, 177);
            this.linkLabel1.Name = "linkLabel1";
            this.linkLabel1.Size = new System.Drawing.Size(53, 12);
            this.linkLabel1.TabIndex = 3;
            this.linkLabel1.TabStop = true;
            this.linkLabel1.Text = "登录系统";
            this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(190, 167);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(234, 46);
            this.label2.TabIndex = 5;
            this.label2.Text = "联系人：周先生\r\n联系电话：0546-7774588 18954609698 ";
            // 
            // MainForm
            // 
            this.ClientSize = new System.Drawing.Size(436, 202);
            this.Controls.Add(this.linkLabel1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.Linelist);
            this.Controls.Add(this.Phonic_usb);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "MainForm";
            this.ShowInTaskbar = false;
            this.Text = "酒店好帮手";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_Closing);
            this.Load += new System.EventHandler(this.MainForm_Load);
            this.SizeChanged += new System.EventHandler(this.MainForm_SizeChanged);
            ((System.ComponentModel.ISupportInitialize)(this.Phonic_usb)).EndInit();
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }




        #region 电话录音

        private enum eColums
        {
            COL_LINE_ID,
            COL_LINE_TYPE,
            COL_STATE,
            COL_CALLER,
            COL_DTMF,
            COL_MAX
        }

        private enum EnumStep
        {
            STEP_IDLE,
            STEP_WELCOME,
            STEP_INPUT_COUNT,
            STEP_INPUT_PASS,
            STEP_CONSULT_DETAIL,
            STEP_GOODBY
        }
        private void OnCallId(object sender, _DPhonic_usbEvents_CallIdEvent e)
        {
            Channel channel = this.GetChannel(e.uboxHandle);
            if (channel != null)
            {
                string phone = e.callerNumber;
                //如果识别的是小号加零了，那么去掉0
                if (phone.Length == 12 & phone.StartsWith("0"))
                {
                    phone = phone.Substring(1, 11);
                }
                //去掉+86
                phone = phone.Replace("+86", "");
                channel._callerNumber = phone;//放到channel中
                if (phone.Length > 0)
                {
                    //this.ShowBalloonTip(phone + "来电");
                    //弹出窗口
                    Form s = new Callin(phone);
                    s.Show();
                }
                this.Linelist.Items[channel._lineID].SubItems[3].Text = phone;
                // Process.Start("IEXPLORE.EXE", "http://localhost:" + _portString + "/ui/callin.aspx?phone=" + channel._callerNumber);
                channel._step = 1L;


               


            }
        }

        private void OnCallOutFinish(object sender, _DPhonic_usbEvents_CallOutFinishEvent e)
        {
            Channel channel = this.GetChannel(e.uboxHandle);
            if (channel != null)
            {
                this.Linelist.Items[channel._lineID].SubItems[2].Text = "呼叫完成";
            }
        }

        private void OnDeviceAlarm(object sender, _DPhonic_usbEvents_DeviceAlarmEvent e)
        {
            Channel channel = this.GetChannel(e.uboxHandle);
            if (channel != null)
            {
                string text = "UBOX ";
                text = text + channel._lineID.ToString();
                switch (e.param)
                {
                    case 1:
                        text = text + "未能找到ubox 的MIC 设备!";
                        break;

                    case 2:
                        text = text + "未能打开ubox 的MIC 设备!";
                        break;

                    case 3:
                        text = text + "未能打开ubox 的放音设备!";
                        break;

                    case 4:
                        text = text + "设备故障!";
                        break;
                }
                ShowError(text);
            }
        }

        private void OnDeviceError(object sender, _DPhonic_usbEvents_DeviceErrorEvent e)
        {
            Channel channel = this.GetChannel(e.uboxHandle);
            if (channel != null)
            {
                ShowError("UBOX " + channel._lineID.ToString() + " 设备错误!");
            }
        }

        private void OnDtmfKey(object sender, _DPhonic_usbEvents_DtmfKeyEvent e)
        {
            Channel channel = this.GetChannel(e.uboxHandle);
            if (channel != null)
            {
                channel._strDTMF = channel._strDTMF + ((char)e.dtmfCode);
                this.Linelist.Items[channel._lineID].SubItems[4].Text = channel._strDTMF;
                channel._callerNumber += (char)e.dtmfCode;
            }
        }

        private void OnHangup(object sender, _DPhonic_usbEvents_HangUpEvent e)
        {
            Channel channel = this.GetChannel(e.uboxHandle);
            if (channel != null)
            {
                this.Linelist.Items[channel._lineID].SubItems[2].Text = "空闲";
                this.Linelist.Items[channel._lineID].SubItems[3].Text = "";
                this.Linelist.Items[channel._lineID].SubItems[4].Text = "";
                channel._ringCount = 0;
                channel._callerNumber = "";
                channel._strDTMF = "";
                if (channel._bRecording)
                {
                    this.Phonic_usb.StopRecord(channel._handle);
                    channel._bRecording = false;
                }
            }
        }

        private void OnHookOff(object sender, _DPhonic_usbEvents_HookOffEvent e)
        {
            Channel channel = this.GetChannel(e.uboxHandle);
            if (channel != null)
            {

                this.Linelist.Items[channel._lineID].SubItems[2].Text = "摘机";
                this.Linelist.Items[channel._lineID].SubItems[3].Text = "";
                this.Linelist.Items[channel._lineID].SubItems[4].Text = "";
                channel._bRecording = true;
                string fileName = this.GetFileName(channel._lineID, channel._callerNumber);
                this.Phonic_usb.RecordFile(channel._handle, fileName, 0);

                if (channel._callerNumber.Length > 0)
                {//如果直接摘机的化没有来电号码，不需要弹窗。
                    //接听来电
                    Get2Http("http://localhost:" + _portString + "/ui/jietinglaidian.aspx?phone=" + channel._callerNumber, System.Text.Encoding.UTF8);

                    string customerinfo = Get2Http("http://localhost:" + _portString + "/ui/getCallNumInfo.aspx?phone=" + channel._callerNumber, System.Text.Encoding.UTF8);
                    if (customerinfo.Trim().Length > 0)
                    {
                        customerinfo += "来电，是否";
                    }

                    StartKiller(10);//无操作10秒后关闭弹出窗口
                    //接听弹窗
                    if (DialogResult.OK == MessageBox.Show(customerinfo + "打开来电速录?", "确认", MessageBoxButtons.OKCancel))
                    {
                        Process.Start("IEXPLORE.EXE", "http://localhost:" + _portString + "/ui/callin.aspx?phone=" + channel._callerNumber + "&close=close");
                    }

                }


            }
        }

        private void OnPlayEnd(object sender, _DPhonic_usbEvents_PlayEndEvent e)
        {
            Channel channel = this.GetChannel(e.uboxHandle);
            if (channel != null)
            {
                this.Linelist.Items[channel._lineID].SubItems[2].Text = "PlayEnd";
            }
        }

        private void OnPlugIn(object sender, _DPhonic_usbEvents_PLugInEvent e)
        {
            Channel channel = new Channel();
            channel.Inital(e.uboxHandle, this.ChannelArray.Count);
            int productID = this.Phonic_usb.GetProductID(e.uboxHandle);
            string str = "";
            switch (productID)
            {
                case 1:
                    str = "FI3102A";
                    break;

                case 2:
                    str = "FI3002B";
                    break;

                case 3:
                    str = "FI3001B";
                    break;

                case 4:
                    str = "FI3101A";
                    break;

                default:
                    str = "unkown product";
                    break;
            }
            ListViewItem item = new ListViewItem(new string[] { this.ChannelArray.Count.ToString(), str, "空闲", "", "" });
            item.Tag = channel;
            this.Linelist.Items.Add(item);
            this.ChannelArray.Add(channel);
        }

        private void OnPlugOut(object sender, _DPhonic_usbEvents_PlugOutEvent e)
        {
            Channel channel = this.GetChannel(e.uboxHandle);
            if (channel != null)
            {
                foreach (ListViewItem item in this.Linelist.Items)
                {
                    if (((Channel)item.Tag) == channel)
                    {
                        item.Remove();
                        this.ChannelArray.Remove(channel);
                        break;
                    }
                }
                for (int i = 0; i < this.Linelist.Items.Count; i++)
                {
                    this.Linelist.Items[i].SubItems[0].Text = i.ToString();
                }

            }
        }
        private void OnRing(object sender, _DPhonic_usbEvents_RingEvent e)
        {
            Channel channel = this.GetChannel(e.uboxHandle);

            if (channel != null)
            {
                channel._ringCount++;
                string str = "振铃 ";
                str = str + channel._ringCount.ToString();
                this.Linelist.Items[channel._lineID].SubItems[2].Text = str;
                //未接来电
                Get2Http("http://localhost:" + _portString + "/ui/weijielaidian.aspx?phone=" + channel._callerNumber, System.Text.Encoding.UTF8);



            }
        }
        private void OnRingCancel(object sender, _DPhonic_usbEvents_RingCancelEvent e)
        {
            Channel channel = this.GetChannel(e.uboxHandle);
            if (channel != null)
            {
                this.Linelist.Items[channel._lineID].SubItems[2].Text = "空闲";
                this.Linelist.Items[channel._lineID].SubItems[3].Text = "";
                this.Linelist.Items[channel._lineID].SubItems[4].Text = "";
                channel._ringCount = 0;
                channel._callerNumber = "";
            }
        }

        private Channel GetChannel(int handle)
        {
            foreach (Channel channel in this.ChannelArray)
            {
                if (channel._handle == handle)
                {
                    return channel;
                }
            }
            return null;
        }

        private string GetFileName(int lineID, string CallPhoneNum)
        {
            DateTime now = DateTime.Now;
            //string path = @"c:\\电话录音\\";
            string path = Application.StartupPath.ToLower();
            string str = now.Year.ToString("d4");
            path = path + @"\\电话录音\\" + str;
            str = str + now.Month.ToString("d2");
            path = path + @"\" + now.Month.ToString("d2");
            str = str + now.Day.ToString("d2");
            path = (path + @"\" + now.Day.ToString("d2")) + @"\" + lineID.ToString("d3");
            Directory.CreateDirectory(path);
            path = path + @"\";
            str = (((str + "-") + now.Hour.ToString("d2") + now.Minute.ToString("d2")) + now.Second.ToString("d4") + "-") + CallPhoneNum + ".wav";

            return (path + str);
        }
        #endregion


        private void MainForm_Load(object sender, EventArgs e)
        {


            ShowBalloonTip("检查更新");
            string address = "www.laihuabao.com";
            Updater updater = new Updater();
            if (updater.Detect(address, 9560))
            {
                updater.Updating(address, 9560, "SmsUpdate.exe", true);
                TerminateProcess(Process.GetCurrentProcess().Handle, 1);
            }
            ShowBalloonTip("启动服务");
            this.Start();


            if (this.Phonic_usb.OpenDevice(0) != 0)
            {
                ShowError("连接设备错误");
            }
            else
            {
                ShowBalloonTip("加载电话驱动");
                this.ChannelArray = new ArrayList();
                this.Linelist.GridLines = true;
                this.Linelist.View = View.Details;
                this.Linelist.FullRowSelect = true;
                this.Linelist.Columns.Add("ID", 60);
                this.Linelist.Columns.Add("类型", 60);
                this.Linelist.Columns.Add("状态", 60);
                this.Linelist.Columns.Add("主叫", 100);
                this.Linelist.Columns.Add("按键", 100);
            }
            ShowBalloonTip("启动完毕，开始工作");




        }

        #region 超链接

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("IEXPLORE.EXE", this.GetLinkText() + this.GetSSOLogin());
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("IEXPLORE.EXE", this.GetLinkText() + "ui/callin.aspx?" + "close=close");
        }
        #endregion





        /// <summary>
        /// 传递参数
        /// </summary>
        /// <param name="url"></param>
        /// <param name="en"></param>
        /// <returns></returns>
        public static string Get2Http(string url, Encoding en)
        {


            try
            {
                HttpWebRequest request = null;


                request = (HttpWebRequest)WebRequest.Create(url);
                request.CookieContainer = new CookieContainer();
                request.Timeout = 20000;
                request.KeepAlive = false;
                request.AllowAutoRedirect = true;
                request.Method = "GET";
                request.ContentType = "application/x-www-form-urlencoded";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), en);
                string str = reader.ReadToEnd();
                reader.Close();
                return str;
            }
            catch (SystemException er)
            {

                return "";
            }

        }













        #region 定時自動关闭MessageBox視窗小技巧
        [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);




        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
        public const int WM_CLOSE = 0x10;

        private void StartKiller(int getTime)
        {
            Timer SetTimer = new Timer();         // 宣告一個Timer物件，名稱為 SetTimer 
            SetTimer.Interval = (getTime * 1000);          //x秒后启动
            SetTimer.Tick += new EventHandler(SetTimer_Tick);   // 建立一个触发Tick事件 
            SetTimer.Start();                     // 启动Timer (即SetTimer 开始启动 ) 
        }


        void SetTimer_Tick(object sender, EventArgs e)
        {
            KillMessageBox();         // 執行把 MessageBox 刪除 (即让MessageBox消失) 
            ((Timer)sender).Stop();    //停止時間。記得要转型为 Timer 型別 
        }


        private void KillMessageBox()
        {
            try
            {
                // 按照MessageBox的标题，找出Messagebox这个视窗  
                IntPtr ptr = FindWindow(null, "确认");
                if (ptr != IntPtr.Zero)    // 此表示只要 ptr 不为空值的内容时 
                {
                    //找到则关闭 MessageBox 視窗  
                    PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }


        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            Form s = new Callin("15605468613c");
            s.Show();
        }
    }
}

